/*     */ package com.lmpop.frame.util;
/*     */ 
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.security.NoSuchProviderException;
/*     */ import java.security.SecureRandom;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Hashtable;
/*     */ import java.util.Random;
/*     */ import java.util.StringTokenizer;
/*     */ 
/*     */ public class RandomStrg
/*     */ {
/*  18 */   private static Integer length = new Integer(8);
/*     */   private static String randomstr;
/*  22 */   private static boolean allchars = false;
/*     */   private static Hashtable hmap;
/*  25 */   private static ArrayList lower = null;
/*  26 */   private static ArrayList upper = null;
/*  27 */   private static char[] single = null;
/*  28 */   private static int singlecount = 0;
/*  29 */   private static boolean singles = false;
/*  30 */   private static String algorithm = null;
/*  31 */   private static String provider = null;
/*  32 */   private static boolean secure = false;
/*  33 */   private static Random random = null;
/*  34 */   private static SecureRandom secrandom = null;
/*     */ 
/*     */   public static void setLength(int count)
/*     */   {
/*  19 */     length = new Integer(count);
/*     */   }
/*     */ 
/*     */   private static float getFloat()
/*     */   {
/*  37 */     if (random == null) {
/*  38 */       return secrandom.nextFloat();
/*     */     }
/*  40 */     return random.nextFloat();
/*     */   }
/*     */ 
/*     */   public static void generateRandomObject()
/*     */     throws Exception
/*     */   {
/*  51 */     if (secure)
/*     */       try
/*     */       {
/*  54 */         if (provider != null)
/*     */         {
/*  56 */           random = SecureRandom.getInstance(algorithm, provider);
/*     */         }
/*  58 */         else random = SecureRandom.getInstance(algorithm); 
/*     */       }
/*     */       catch (NoSuchAlgorithmException ne) {
/*  60 */         throw new Exception(ne.getMessage());
/*     */       } catch (NoSuchProviderException pe) {
/*  62 */         throw new Exception(pe.getMessage());
/*     */       }
/*     */     else
/*  65 */       random = new Random();
/*     */   }
/*     */ 
/*     */   private static void generaterandom()
/*     */   {
/*  74 */     if (allchars) {
/*  75 */       for (int i = 0; i < length.intValue(); i++)
/*  76 */         randomstr += new Character((char)(34 + (int)(getFloat() * 93.0F))).toString();
/*     */     }
/*  78 */     else if (singles)
/*     */     {
/*  81 */       if (upper.size() == 3)
/*     */       {
/*  85 */         for (int i = 0; i < length.intValue(); i++)
/*     */         {
/*  90 */           if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */           {
/*  93 */             if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */             {
/*  95 */               randomstr += randomSingle().toString();
/*     */             }
/*     */             else {
/*  98 */               randomstr += randomChar((Character)lower.get(2), (Character)upper.get(2)).toString();
/*     */             }
/*     */ 
/*     */           }
/* 103 */           else if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */           {
/* 105 */             randomstr += randomChar((Character)lower.get(1), (Character)upper.get(1)).toString();
/*     */           }
/*     */           else
/*     */           {
/* 109 */             randomstr += randomChar((Character)lower.get(0), (Character)upper.get(0)).toString();
/*     */           }
/*     */         }
/*     */       }
/* 113 */       else if (upper.size() == 2)
/*     */       {
/* 118 */         for (int i = 0; i < length.intValue(); i++)
/*     */         {
/* 122 */           if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */           {
/* 125 */             randomstr += randomSingle().toString();
/* 126 */           } else if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */           {
/* 129 */             randomstr += randomChar((Character)lower.get(1), (Character)upper.get(1)).toString();
/*     */           }
/*     */           else
/*     */           {
/* 134 */             randomstr += randomChar((Character)lower.get(0), (Character)upper.get(0)).toString();
/*     */           }
/*     */         }
/*     */       }
/* 138 */       else if (upper.size() == 1)
/*     */       {
/* 141 */         for (int i = 0; i < length.intValue(); i++) {
/* 142 */           if ((int)getFloat() * 100 % 2 == 0)
/*     */           {
/* 144 */             randomstr += randomSingle().toString();
/*     */           }
/*     */           else {
/* 147 */             randomstr += randomChar((Character)lower.get(0), (Character)upper.get(0)).toString();
/*     */           }
/*     */         }
/*     */       }
/*     */       else {
/* 152 */         for (int i = 0; i < length.intValue(); i++) {
/* 153 */           randomstr += randomSingle().toString();
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/* 158 */     else if (upper.size() == 3)
/*     */     {
/* 161 */       for (int i = 0; i < length.intValue(); i++)
/*     */       {
/* 163 */         if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */         {
/* 166 */           randomstr += randomChar((Character)lower.get(2), (Character)upper.get(2)).toString();
/*     */         }
/* 168 */         else if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */         {
/* 171 */           randomstr += randomChar((Character)lower.get(1), (Character)upper.get(1)).toString();
/*     */         }
/*     */         else
/*     */         {
/* 176 */           randomstr += randomChar((Character)lower.get(0), (Character)upper.get(0)).toString();
/*     */         }
/*     */       }
/*     */     }
/* 180 */     else if (upper.size() == 2)
/*     */     {
/* 183 */       for (int i = 0; i < length.intValue(); i++) {
/* 184 */         if ((int)(getFloat() * 100.0F) % 2 == 0)
/*     */         {
/* 186 */           randomstr += randomChar((Character)lower.get(1), (Character)upper.get(1)).toString();
/*     */         }
/*     */         else
/*     */         {
/* 190 */           randomstr += randomChar((Character)lower.get(0), (Character)upper.get(0)).toString();
/*     */         }
/*     */       }
/*     */     }
/*     */     else
/*     */     {
/* 196 */       for (int i = 0; i < length.intValue(); i++)
/*     */       {
/* 198 */         randomstr += randomChar((Character)lower.get(0), (Character)upper.get(0)).toString();
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private static Character randomSingle()
/*     */   {
/* 211 */     return new Character(single[(int)(getFloat() * singlecount - 1.0F)]);
/*     */   }
/*     */ 
/*     */   private static Character randomChar(Character lower, Character upper)
/*     */   {
/* 224 */     char low = lower.charValue();
/* 225 */     char up = upper.charValue();
/*     */ 
/* 228 */     int tempval = (int)(low + getFloat() * (up - low));
/*     */ 
/* 231 */     return new Character((char)tempval);
/*     */   }
/*     */ 
/*     */   public static String getRandom()
/*     */   {
/* 243 */     randomstr = new String();
/*     */ 
/* 245 */     generaterandom();
/*     */ 
/* 247 */     if (hmap != null)
/*     */     {
/* 249 */       while (hmap.containsKey(randomstr))
/*     */       {
/* 251 */         generaterandom();
/*     */       }
/*     */ 
/* 254 */       hmap.put(randomstr, null);
/*     */     }
/*     */ 
/* 257 */     return randomstr;
/*     */   }
/*     */ 
/*     */   public static void setRanges(ArrayList low, ArrayList up)
/*     */   {
/* 268 */     lower = low;
/* 269 */     upper = up;
/*     */   }
/*     */ 
/*     */   public static void setHmap(Hashtable map)
/*     */   {
/* 280 */     hmap = map;
/*     */   }
/*     */ 
/*     */   public static void setLength(String value)
/*     */   {
/* 290 */     length = new Integer(value);
/*     */   }
/*     */ 
/*     */   public static void setAlgorithm(String value)
/*     */   {
/* 301 */     algorithm = value;
/* 302 */     secure = true;
/*     */   }
/*     */ 
/*     */   public static void setProvider(String value)
/*     */   {
/* 312 */     provider = value;
/*     */   }
/*     */ 
/*     */   public static void setAllchars(boolean value)
/*     */   {
/* 322 */     allchars = value;
/*     */   }
/*     */ 
/*     */   public static void setSingle(char[] chars, int value)
/*     */   {
/* 334 */     single = chars;
/* 335 */     singlecount = value;
/* 336 */     singles = true;
/*     */   }
/*     */ 
/*     */   public static void setCharset(String value)
/*     */   {
/* 342 */     boolean more = true;
/*     */ 
/* 346 */     lower = new ArrayList(3);
/* 347 */     upper = new ArrayList(3);
/*     */ 
/* 350 */     if (value.compareTo("all") == 0) {
/* 351 */       allchars = true;
/*     */ 
/* 354 */       more = false;
/* 355 */     } else if ((value.charAt(1) == '-') && (value.charAt(0) != '\\'))
/*     */     {
/* 357 */       while ((more) && (value.charAt(1) == '-'))
/*     */       {
/* 360 */         if (value.charAt(0) == '\\')
/*     */         {
/*     */           break;
/*     */         }
/* 364 */         lower.add(new Character(value.charAt(0)));
/* 365 */         upper.add(new Character(value.charAt(2)));
/*     */ 
/* 369 */         if (value.length() <= 3) {
/* 370 */           more = false; continue;
/*     */         }
/*     */ 
/* 374 */         value = value.substring(3);
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 379 */     if (more)
/*     */     {
/* 381 */       single = new char[30];
/*     */ 
/* 384 */       StringTokenizer tokens = new StringTokenizer(value);
/*     */ 
/* 386 */       while (tokens.hasMoreTokens())
/*     */       {
/* 388 */         String token = tokens.nextToken();
/*     */ 
/* 390 */         if (token.length() > 1)
/*     */         {
/* 392 */           single[(singlecount++)] = '-';
/*     */         }
/*     */ 
/* 395 */         single[(singlecount++)] = token.charAt(0);
/*     */       }
/*     */     }
/* 398 */     if ((lower == null) && (single == null))
/* 399 */       setCharset("a-zA-Z0-9");
/*     */   }
/*     */ }

/* Location:           F:\电商\commons-utils.jar
 * Qualified Name:     com.lmpop.frame.util.RandomStrg
 * JD-Core Version:    0.6.0
 */